package com.example.studyPlatform.Filter;


import com.example.studyPlatform.bean.User;
import com.example.studyPlatform.config.ThemeConfig;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static com.example.studyPlatform.config.BaseConstant.HTML_SUFFIX;
import static com.example.studyPlatform.config.BaseConstant.USR;
import static com.example.studyPlatform.config.RoutingConstant.LOGIN;
import static com.example.studyPlatform.config.RoutingConstant.USER;


@WebFilter(filterName = "userFilter", urlPatterns = USER + "/*")
public class UserFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) req;
        HttpSession session = httpRequest.getSession();
        User user = (User) session.getAttribute(USR);
        if (user != null && user.getId() != -1) chain.doFilter(req, resp);
        else ((HttpServletResponse) resp).sendRedirect(LOGIN+HTML_SUFFIX);
    }
}
